#include <GL/glut.h>
#define xuser mixc::powerful_cat
#include"extern/temprh/temprh.hpp"
#include"mixc.hpp"
#define inc xuser

struct rgb{
    u08 r;
    u08 g;
    u08 b;
};

#define xlcd_scale      (4)
#define xlcd_pix_margin (1)
#define xlcd_w          (160)
#define xlcd_h          (160)

#define xlcd_padding    (40)
#define xbuf_w          (((xlcd_scale + xlcd_pix_margin) * xlcd_w) + xlcd_padding * 2)
#define xbuf_h          (((xlcd_scale + xlcd_pix_margin) * xlcd_h) + xlcd_padding * 2)

struct lcd_color_t{
    static inline constexpr rgb background{ .r = 0xc0, .g = 0xc0, .b = 0x40 };
    static inline constexpr rgb black{ .r = 0x50, .g = 0x50, .b = 0x40 };
    static inline constexpr rgb empty{ .r = 0xa0, .g = 0xa0, .b = 0x50 };
} color;

rgb rgb_pixel[xbuf_h][xbuf_w];

void set_pixel(uxx x, uxx y, rgb value){
    for(uxx i_y = y * (xlcd_scale + xlcd_pix_margin) + xlcd_padding, end = i_y + xlcd_scale; i_y < end; i_y++){
        for(uxx i_x = x * (xlcd_scale + xlcd_pix_margin) + xlcd_padding, end = i_x + xlcd_scale; i_x < end; i_x++){
            rgb_pixel[i_y][i_x] = value;
        }
    }
}

void display() {
    glDrawPixels(xbuf_w, xbuf_h, GL_RGB, GL_UNSIGNED_BYTE, rgb_pixel);
    glFlush();
}

xinit(inc::the_main){
    glutInit(&inc::env::c::argc, inc::env::c::argv);
    glutInitWindowSize(xbuf_w, xbuf_h);

    // 居中显示
    auto max_screen_w = glutGet(GLUT_SCREEN_WIDTH);
    auto max_screen_h = glutGet(GLUT_SCREEN_HEIGHT);
    glutInitWindowPosition((max_screen_w - xbuf_w) / 2, (max_screen_h - xbuf_h) / 2);

    // 先 Init，后 create，不然大小和位置不会变化
    glutCreateWindow("DOT LCD");

    // 黄绿背景色
    for(int i = 0; i < xbuf_h; i++){
        for(int j = 0; j < xbuf_w; j++){
            rgb_pixel[i][j] = color.background;
        }
    }

    // 设置颜色
    // for(int i_y = 0; i_y < xlcd_h / 2; i_y++){
    //     for(int i_x = 0; i_x < xlcd_w; i_x++){
    //         set_pixel(i_x, i_y, color.black);
    //     }
    // }

    // 关闭颜色
    for(int i_y = 0; i_y < xlcd_h; i_y++){
        for(int i_x = 0; i_x < xlcd_w; i_x++){
            set_pixel(i_x, i_y, color.empty);
        }
    }

    inc::temprh         th;
    inc::temprh_conf    conf{
        .origin = { 0, 0 },
        .temp = {
            .degree = 0,
            .int_part = {
                .cell_width = 10,
                .cell_padding = 0,
                .cell_margin = 1,
                .cell_fill = true,
            },

            .decimal_part = {
                .cell_width = 5,
                .cell_padding = 0,
                .cell_margin = 1,
                .cell_fill = true,
            },
        }
    };

    struct screen_wrapper{
        void set_pixel(uxx x, uxx y, bool value) const {
            ::set_pixel(x, y, value ? color.empty : color.black);
        }
    } device;

    conf.temp.degree = 25.4f;
    th.draw(conf, device);

    glutDisplayFunc(display); // Register display callback handler for window re-paint
    glutMainLoop();           // Enter the event-processing loop
};
